<?php
namespace Tbf\Bundle\CronBundle\Tests;
use Tbf\Bundle\CronBundle\Entity\Cron;
use Tbf\Bundle\CronBundle\Entity\CronRepository;

class CronRepositoryTest extends CronBundleTestCase
{
    public function testPersist()
    {
        $this->setDbData(array(
            'tbf_cron'=>array()
        ));
        $cron = $this->newOnceCron(new \DateTime());
        $this->getRepository()->persist($cron);
        $data = $this->getTableData('tbf_cron');
        $this->assertEquals(1,count($data));
        $this->_testRemove($cron);
    }
    public function _testRemove($cron)
    {
        $this->getRepository()->remove($cron);
        $data = $this->getTableData('tbf_cron');
        $this->assertEquals(0,count($data));
    }
    public function testGetNeedRunList()
    {
        $this->getRepository()->persist($this->newOnceCron(\DateTime::createFromFormat('Y-m-d H:i:s','2000-01-01 00:00:00')));
        $this->getRepository()->persist($this->newOnceCron(\DateTime::createFromFormat('Y-m-d H:i:s','2010-01-01 00:00:00')));
        $this->getRepository()->persist($this->newOnceCron(\DateTime::createFromFormat('Y-m-d H:i:s','2020-01-01 00:00:00')));
        $data = $this->getRepository()->getNeedRunList(\DateTime::createFromFormat('Y-m-d H:i:s','2010-01-01 00:00:00'));
        $this->assertEquals(2,count($data));
    }
    public function newOnceCron(\DateTime $time)
    {
        $date_list = $this->newDateListMock();
        $date_list->expects($this->once())->method('current')->will($this->returnValue($time));
        $task = $this->newTaskMock();

        return new Cron($date_list,$task);
    }
    /**
     * @return \Tbf\Bundle\CronBundle\Entity\CronRepository
     */
    public function getRepository()
    {
        return $this->getClientDic()->get('doctrine')->getRepository('TbfCronBundle:Cron');
    }
}
